<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>收缩菜单</title>
    <style>
        html,
        body,
        ul,
        li,
        h2 {
            margin: 0;
            padding: 0;
        }
        
        h2 {
            padding: 4px 20px;
            font-size: 18px;
            line-height: 18px;
            background: #2EAAC0 url('./img/ico1.gif') 5px 10px no-repeat;
            cursor: pointer;
        }
        
        li {
            list-style-type: none;
        }
        
        div {
            border: 1px solid #AAA8A8;
            width: 240px;
            height: 400px;
        }
        
        .group {
            display: none;
        }
        
        .unit {
            padding: 4px 25px;
            background-color: #fff;
            cursor: pointer;
        }
        
        .group-checked {
            background: #D0E946 url('./img/ico2.gif') 5px 10px no-repeat;
        }
        
        .unit-checked {
            background-color: #2EAAC0;
        }
    </style>
</head>

<body>
    <div>
        <span>
            <h2>我的好友</h2>
            <ul class="group">
                <li class="unit">张三1</li>
                <li class="unit">张三2</li>
                <li class="unit">张三3</li>
                <li class="unit">张三4</li>
            </ul>
        </span>
        <span>
            <h2>企业好友</h2>
            <ul class="group">
                <li class="unit">李四1</li>
                <li class="unit">李四2</li>
                <li class="unit">李四3</li>
                <li class="unit">李四4</li>
                <li class="unit">李四5</li>
            </ul>
        </span>
        <span>
            <h2>黑名单</h2>
            <ul class="group">
                <li class="unit">王五1</li>
                <li class="unit">王五2</li>
            </ul>
        </span>
    </div>
    <script>
        var titles = document.getElementsByTagName('h2');
        var groups = document.getElementsByTagName('ul');
        var units;
        for (var i = 0; i < titles.length; i++) {
            titles[i].index = i;
            titles[i].onclick = function() {
                for (var i = 0; i < titles.length; i++) {
                    if (i != this.index && titles[i].className != '') {
                        titles[i].className = '';
                        groups[i].style.display = 'none';
                    }
                }
                if (this.className == '') {
                    this.className = 'group-checked';
                    groups[this.index].style.display = 'block';
                } else {
                    this.className = '';
                    groups[this.index].style.display = 'none';
                }
            }
            doLi(groups[i].getElementsByTagName('li'));
        }

        function doLi(units) {
            for (var j = 0; j < units.length; j++) {
                units[j].onclick = function() {
                    for (var j = 0; j < units.length; j++) {
                        if (units[j] != this && units[j].className != 'unit') {
                            units[j].className = 'unit';
                        }
                    }
                    if (this.className == 'unit') {
                        this.className = 'unit unit-checked';
                    } else {
                        this.className = 'unit';
                    }
                }
            }
        }
    </script>
</body>

</html>